package com.yulongtian.weekCompetition.month202301.week02;


/**
 * 参考他人题解
 *
 * @author yulongTian
 * @create 2023-01-08 10:57
 */
public class Test03 {
    public static void main(String[] args) {
        System.out.println(isItPossible("a", "bb"));
    }

    public static boolean isItPossible(String word1, String word2) {
        char[] chs1 = word1.toCharArray();
        char[] chs2 = word2.toCharArray();
        int[] cnt1 = new int[26];
        int[] cnt2 = new int[26];
        for (char c : chs1) {
            cnt1[c - 'a']++;
        }
        for (char c : chs2) {
            cnt2[c - 'a']++;
        }

        for (int i = 0; i < 26; i++) {
            if (cnt1[i] != 0) {
                for (int j = 0; j < 26; j++) {
                    if (cnt2[j] != 0) {
                        //交换字符
                        cnt1[i]--;
                        cnt1[j]++;
                        cnt2[j]--;
                        cnt2[i]++;
                        //检查交换后是否符合条件
                        int x = 0;
                        int y = 0;
                        for (int i1 : cnt1) {
                            if (i1 != 0) {
                                x++;
                            }
                        }
                        for (int i1 : cnt2) {
                            if (i1 != 0) {
                                y++;
                            }
                        }
                        if (x == y) {
                            return true;
                        }
                        //撤销交换
                        cnt1[i]++;
                        cnt1[j]--;
                        cnt2[j]++;
                        cnt2[i]--;
                    }
                }
            }
        }
        return false;
    }


}
